package find;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class OrganizeMatched {

	static String matched = "sample_match.txt";
	static String cleaned = "sample_match_pretty.txt";
	static HashMap<String, Set<String>> holder = new HashMap<String, Set<String>>();

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new FileReader(matched));
		BufferedWriter bw = new BufferedWriter(new FileWriter(cleaned));
		
		String line = "";
		Set<String> tset;
		line = br.readLine();
		while (line != null) {
			if (!line.equals("")) {
				if (holder.containsKey(line)) {
					tset = holder.get(line);
				} else {
					tset = new HashSet<String>();
					holder.put(line, tset);
				}
				line = br.readLine();
				tset.add(line);
			}
			line = br.readLine();
		}
		
		for (String str : holder.keySet()) {
			bw.write(str);
			bw.newLine();
			tset = holder.get(str);
			for (String t : tset) {
				bw.write(t);
				bw.newLine();
			}
			bw.newLine();
		}
		br.close();
		bw.close();
	}

}
